from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from toast.models import *
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.conf import settings
from datetime import datetime
from django.template import *
from toast.forms import *
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.decorators import login_required
from django.forms.models import inlineformset_factory
from django.contrib.auth import authenticate, login
from django.db.models import Count
import re

def about(request):
	return render_to_response(
			"toast/static/about.html",
			context_instance=RequestContext(request)
		)

def help(request):
	return render_to_response(
			"toast/static/help.html",
			context_instance=RequestContext(request)
		)	


###################################
#	MEETING VIEWS
###################################
@csrf_protect
def meeting_default(request):
	if request.method=="POST":
		form=MeetingForm(request.POST)
		if form.is_valid():
			prepared=form.save(commit=False)
			if len(Meeting.objects.filter(name__exact=prepared.name))>0:
				prepared.save()
	form=MeetingForm
	list_of_meetings = Meeting.objects.all().order_by("-start_datetime")
	return render_to_response(
			"toast/meeting/index.html",
			{
				"list_of_meetings":list_of_meetings,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def meeting_view(request, meeting_id=None):
	if meeting_id:
		form=AttendanceForm
		meeting=Meeting.objects.get(id=meeting_id)
		list_of_attendance={}
		for attendance in Attendance.objects.filter(meeting=meeting_id):
			if attendance.member.id in list_of_attendance:
				list_of_attendance[attendance.member.id].append(attendance)
			else:
				list_of_attendance[attendance.member.id]=[attendance]
		return render_to_response(
				"toast/meeting/view.html",
				{
					"meeting":meeting,
					"list_of_attendance":list_of_attendance,
					"form":form,
				},
				context_instance=RequestContext(request)
			)
	else:
		return HttpResponseRedirect("/toast/meeting/")

@csrf_protect
def meeting_edit(request, meeting_id=None):
	if meeting_id:
		meeting=Meeting.objects.get(id=meeting_id)
		if request.method=="POST":
			form=MeetingForm(request.POST, instance=meeting)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/meeting/view/"+meeting_id)
		else:
			form=MeetingForm(instance=meeting)
			return render_to_response(
					"toast/meeting/edit.html",
					{
						"meeting":meeting,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/meeting/")

@csrf_protect
def _meeting_add(request):
	if request.method=="POST":
		form=MeetingForm(request.POST)
		if form.is_valid():
			new_meeting=form.save()
			return HttpResponseRedirect("/toast/meeting/view/"+str(new_meeting.id))
	return HttpResponseRedirect("/toast/meeting/")


def _meeting_delete(request, meeting_id=None):
	if meeting_id:
		Meeting.objects.get(id=meeting_id).delete()
	return HttpResponseRedirect("/toast/meeting/")






###################################
#	MEMBER VIEWS
###################################
@csrf_protect
def member_default(request):
	form=MemberForm
	list_of_members=Member.objects.all()
	return render_to_response(
			"toast/member/index.html",
			{
				"list_of_members":list_of_members,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def member_view(request, member_id=None):
	if member_id:
		form=MemberForm
		member=Member.objects.get(id=member_id)
		list_of_attendance=Attendance.objects.filter(member=member_id)
		return render_to_response(
				"toast/member/view.html",
				{
					"member":member,
					"list_of_attendance":list_of_attendance,
					"form":form,
				},
				context_instance=RequestContext(request)
			)
	return HttpResponseRedirect("/toast/member/")

@csrf_protect
def member_edit(request, member_id=None):
	if member_id:
		member=Member.objects.get(id=member_id)
		if request.method=="POST":
			form=MemberForm(request.POST, instance=member)
			if form.is_valid():
				form.save()
				return HttpResponseRedirect("/toast/member/")
		else:
			form=MemberForm(instance=member)
			return render_to_response(
					"toast/member/edit.html",
					{
						"member":member,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/member/")

@csrf_protect
def _member_add(request):
	if request.method=="POST":
		form=MemberForm(request.POST)
		if form.is_valid():
			assigned_member=form.save()
			return HttpResponseRedirect("/toast/meeting/view/{{ assigned_member.meeting }}")
	return HttpResponseRedirect("/toast/member/")

def _member_delete(request, member_id=None):
	if member_id:
		Member.objects.get(id=member_id).delete()
	return HttpResponseRedirect("/toast/member/")






###################################
#	ATTENDANCE VIEWS
###################################
@csrf_protect
def attendance_default(request):
	if request.method=="POST":
		form=ParticipationForm(request.POST)
		if form.is_valid():
			form.save()
	form=ParticipationForm
	list_of_participation = Participation.objects.all()
	return render_to_response(
				"toast/attendance/index.html",
				{
					"list_of_participation":list_of_participation,
					"form":form,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def attendance_view(request, attendance_id=None):
	if attendance_id:
		if request.method=="POST":
			form=ParticipationForm(request.POST)
			if form.is_valid():
				form.save()
		form=ParticipationForm
		attendance=Attendance.objects.get(id=attendance_id)
		list_of_participation=Participation.objects.filter(attendance=attendance_id)
		return render_to_response(
					"toast/attendance/view.html",
					{
						"attendance":attendance,
						"list_of_participation":list_of_participation,
						"form":form,
					},
					context_instance=RequestContext(request)
				)

@csrf_protect
def attendance_edit(request, attendance_id=None):
	if attendance_id:
		attendance=Attendance.objects.get(id=attendance_id)
		if request.method=="POST":
			form=AttendanceForm(request.POST, instance=attendance)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/meeting/view/"+str(attendance.meeting.id))
		else:
			form=AttendanceForm(instance=attendance)
			return render_to_response(
					"toast/attendance/edit.html",
					{
						"attendance":attendance,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/attendance/")

@csrf_protect
def _attendance_add(request):
	if request.method=="POST":
		form=AttendanceForm(request.POST)
		if form.is_valid():
			new_attendance=form.save()
			meeting_id=new_attendance.meeting.id
			return HttpResponseRedirect("/toast/meeting/view/"+str(meeting_id))
	return HttpResponseRedirect("/toast/meeting/")

def _attendance_delete(request, attendance_id=None):
	if attendance_id:
		attendance=Attendance.objects.get(id=attendance_id)
		meeting_id=attendance.meeting.id
		attendance.delete()
	return HttpResponseRedirect("/toast/meeting/view/"+str(meeting_id))









###################################
#	PARTICIPATION VIEWS
###################################
@csrf_protect
def participation_default(request):
	if request.method=="POST":
		form=TrackSoundForm(request.POST)
		if form.is_valid():
			form.save()
	form=TrackSoundForm
	list_of_participation = Participation.objects.all()
	return render_to_response(
				"toast/attendance/index.html",
				{
					"list_of_participation":list_of_participation,
					"form":form,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def participation_view(request, participation_id=None):
	if participation_id:
		if request.method=="POST":
			form=TrackSoundForm(request.POST)
			if form.is_valid():
				form.save()
		form=TrackSoundForm
		participation=Participation.objects.get(id=participation_id)
		list_of_soundtracks=TrackSound.objects.filter(participant=participation_id)
		return render_to_response(
					"toast/participation/view.html",
					{
						"participation":participation,
						"list_of_soundtracks":list_of_soundtracks,
						"form":form,
					},
					context_instance=RequestContext(request)
				)

@csrf_protect
def participation_edit(request, participation_id=None):
	if participation_id:
		participation=Participation.objects.get(id=participation_id)
		attendance_id=participation.attendance.id
		if request.method=="POST":
			form=ParticipationForm(request.POST, instance=participation)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/attendance/view/"+str(attendance_id))
		else:
			form=ParticipationForm(instance=participation)
			return render_to_response(
					"toast/participation/edit.html",
					{
						"participation":participation,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/attendance/")

@csrf_protect
def _participation_add(request):
	if request.method=="POST":
		form=ParticipationForm(request.POST)
		if form.is_valid():
			new_participation=form.save(commit=False)
			if new_participation.name=='':
				new_participation.name=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
			new_participation.save()
			attendance_id=new_participation.attendance.id
			return HttpResponseRedirect("/toast/attendance/view/"+str(attendance_id))
	return HttpResponseRedirect("/toast/meeting/")


def _participation_delete(request, participation_id=None):
	if participation_id:
		participation=Participation.objects.get(id=participation_id)
		attendance_id=participation.attendance.id
		participation.delete()
	return HttpResponseRedirect("/toast/attendance/view/"+str(attendance_id))
		


@csrf_protect
def tracksound_default(request):
	form=TrackSoundForm
	list_of_tracksounds=TrackSound.objects.all()
	return render_to_response(
			"toast/tracksound/index.html",
			{
				"list_of_tracksounds":list_of_tracksounds,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def tracksound_view(request, tracksound_id=None):
	if tracksound_id:
		tracksound=TrackSound.objects.get(id=tracksound_id)
		return render_to_response(
					"toast/tracksound/view.html",
					{
						"tracksound":tracksound,
					},
					context_instance=RequestContext(request)
				)


def _tracksound_increment(request, tracksound_id=None):
	if tracksound_id:
		tracksound=TrackSound.objects.get(id=tracksound_id)
		tracksound.count+=1
		tracksound.save()
		participation_id=tracksound.participant.id
		return HttpResponseRedirect("/toast/participation/view/"+str(participation_id))
	return HttpResponseRedirect("/toast/meeting/")

def _tracksound_decrement(request, tracksound_id=None):
	if tracksound_id:
		tracksound=TrackSound.objects.get(id=tracksound_id)
		if tracksound.count>0:
			tracksound.count-=1
		tracksound.save()
		participation_id=tracksound.participant.id
		return HttpResponseRedirect("/toast/participation/view/"+str(participation_id))
	return HttpResponseRedirect("/toast/meeting/")

def _tracksound_zero(request, tracksound_id=None):
	if tracksound_id:
		tracksound=TrackSound.objects.get(id=tracksound_id)
		tracksound.count=0
		tracksound.save()
		participation_id=tracksound.participant.id
		return HttpResponseRedirect("/toast/participation/view/"+str(participation_id))
	return HttpResponseRedirect("/toast/meeting/")

@csrf_protect
def tracksound_edit(request, tracksound_id=None):
	if tracksound_id:
		tracksound=TrackSound.objects.get(id=tracksound_id)
		participation_id=tracksound.participant.id
		if request.method=="POST":
			form=TrackSoundForm(request.POST, instance=tracksound)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/participation/view/"+str(participation_id))
		else:
			form=TrackSoundForm(instance=tracksound)
			return render_to_response(
					"toast/tracksound/edit.html",
					{
						"tracksound":tracksound,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/tracksound/")

@csrf_protect
def _tracksound_add(request):
	if request.method=="POST":
		form=TrackSoundForm(request.POST)
		if form.is_valid():
			new_tracksound=form.save(commit=False)
			new_tracksound.count=0
			new_tracksound.save()
			participation_id=new_tracksound.participant.id
			return HttpResponseRedirect("/toast/participation/view/"+str(participation_id))
	return HttpResponseRedirect("/toast/meeting/")

def _tracksound_delete(request, tracksound_id=None):
	if tracksound_id:
		tracksound=TrackSound.objects.get(id=tracksound_id)
		participation_id=tracksound.participant.id
		tracksound.delete()
	return HttpResponseRedirect("/toast/participation/view/"+str(participation_id))





@csrf_protect
def role_default(request):
	if request.method=="POST":
		form=RoleForm(request.POST)
		if form.is_valid():
			form.save()
	form=RoleForm
	list_of_roles=Role.objects.all()
	return render_to_response(
			"toast/role/index.html",
			{
				"list_of_roles":list_of_roles,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def role_view(request, role_id=None):
	if role_id:
		form=RoleForm
		role=Role.objects.get(id=role_id)
		return render_to_response(
				"toast/role/view.html",
				{
					"role":role,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def role_edit(request, role_id=None):
	if role_id:
		role=Role.objects.get(id=role_id)
		if request.method=="POST":
			form=RoleForm(request.POST, instance=role)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/role/")
		else:
			form=RoleForm(instance=role)
			return render_to_response(
					"toast/role/edit.html",
					{
						"role":role,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/role/")

@csrf_protect
def _role_add(request):
	if request.method=="POST":
		form=RoleForm(request.POST)
		if form.is_valid():
			new_role=form.save()
	return HttpResponseRedirect("/toast/role/")

def _role_delete(request, role_id=None):
	if role_id:
		Role.objects.get(id=role_id).delete()
		return HttpResponseRedirect("/toast/role/")









@csrf_protect
def address_default(request):
	if request.method=="POST":
		form=AddressForm(request.POST)
		if form.is_valid():
			form.save()
	form=AddressForm
	list_of_addresses=Address.objects.all()
	return render_to_response(
			"toast/address/index.html",
			{
				"list_of_addresses":list_of_addresses,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def address_view(request, address_id=None):
	if address_id:
		form=AddressForm
		address=Address.objects.get(id=address_id)
		return render_to_response(
				"toast/address/view.html",
				{
					"address":address,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def address_edit(request, address_id=None):
	if address_id:
		address=Address.objects.get(id=address_id)
		if request.method=="POST":
			form=AddressForm(request.POST, instance=address)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/address/")
		else:
			form=AddressForm(instance=address)
			return render_to_response(
					"toast/address/edit.html",
					{
						"address":address,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/address/")

@csrf_protect
def _address_add(request):
	if request.method=="POST":
		form=AddressForm(request.POST)
		if form.is_valid():
			new_address=form.save()
	return HttpResponseRedirect("/toast/address/")

def _address_delete(request, address_id=None):
	if address_id:
		Address.objects.get(id=address_id).delete()
		return HttpResponseRedirect("/toast/address/")











@csrf_protect
def sound_default(request):
	if request.method=="POST":
		form=SoundForm(request.POST)
		if form.is_valid():
			form.save()
	form=SoundForm
	list_of_sounds=Sound.objects.all()
	return render_to_response(
			"toast/sound/index.html",
			{
				"list_of_sounds":list_of_sounds,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def sound_view(request, sound_id=None):
	if sound_id:
		form=SoundForm
		sound=Sound.objects.get(id=sound_id)
		return render_to_response(
				"toast/sound/view.html",
				{
					"sound":sound,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def sound_edit(request, sound_id=None):
	if sound_id:
		sound=Sound.objects.get(id=sound_id)
		if request.method=="POST":
			form=SoundForm(request.POST, instance=sound)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/sound/")
		else:
			form=SoundForm(instance=sound)
			return render_to_response(
					"toast/sound/edit.html",
					{
						"sound":sound,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/sound/")

@csrf_protect
def _sound_add(request):
	if request.method=="POST":
		form=SoundForm(request.POST)
		if form.is_valid():
			new_sound=form.save()
	return HttpResponseRedirect("/toast/sound/")

def _sound_delete(request, sound_id=None):
	if sound_id:
		Sound.objects.get(id=sound_id).delete()
		return HttpResponseRedirect("/toast/sound/")









@csrf_protect
def participation_type_default(request):
	if request.method=="POST":
		form=ParticipationTypeForm(request.POST)
		if form.is_valid():
			form.save()
	form=ParticipationTypeForm
	list_of_participation_types=ParticipationType.objects.all()
	return render_to_response(
			"toast/participation_type/index.html",
			{
				"list_of_participation_types":list_of_participation_types,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def participation_type_view(request, participation_type_id=None):
	if participation_type_id:
		form=ParticipationTypeForm
		participation_type=ParticipationType.objects.get(id=participation_type_id)
		return render_to_response(
				"toast/participation_type/view.html",
				{
					"participation_type":participation_type,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def participation_type_edit(request, participation_type_id=None):
	if participation_type_id:
		participation_type=ParticipationType.objects.get(id=participation_type_id)
		if request.method=="POST":
			form=ParticipationTypeForm(request.POST, instance=participation_type)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/participation_type/")
		else:
			form=ParticipationTypeForm(instance=participation_type)
			return render_to_response(
					"toast/participation_type/edit.html",
					{
						"participation_type":participation_type,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/participation_type/")

@csrf_protect
def _participation_type_add(request):
	if request.method=="POST":
		form=ParticipationTypeForm(request.POST)
		if form.is_valid():
			new_participation_type=form.save()
	return HttpResponseRedirect("/toast/participation_type/")

def _participation_type_delete(request, participation_type_id=None):
	if participation_type_id:
		ParticipationType.objects.get(id=participation_type_id).delete()
		return HttpResponseRedirect("/toast/participation_type/")











@csrf_protect
def state_default(request):
	if request.method=="POST":
		form=StateForm(request.POST)
		if form.is_valid():
			form.save()
	form=StateForm
	list_of_states=State.objects.all()
	return render_to_response(
			"toast/state/index.html",
			{
				"list_of_states":list_of_states,
				"form":form,
			},
			context_instance=RequestContext(request)
		)

@csrf_protect
def state_view(request, state_id=None):
	if state_id:
		form=StateForm
		state=State.objects.get(id=state_id)
		return render_to_response(
				"toast/state/view.html",
				{
					"state":state,
				},
				context_instance=RequestContext(request)
			)

@csrf_protect
def state_edit(request, state_id=None):
	if state_id:
		state=State.objects.get(id=state_id)
		if request.method=="POST":
			form=StateForm(request.POST, instance=state)
			if form.is_valid():
				form.save()
			return HttpResponseRedirect("/toast/state/")
		else:
			form=StateForm(instance=state)
			return render_to_response(
					"toast/state/edit.html",
					{
						"state":state,
						"form":form,
					},
					context_instance=RequestContext(request)
				)
	else:
		return HttpResponseRedirect("/toast/state/")

@csrf_protect
def _state_add(request):
	if request.method=="POST":
		form=StateForm(request.POST)
		if form.is_valid():
			new_state=form.save()
	return HttpResponseRedirect("/toast/state/")

def _state_delete(request, state_id=None):
	if state_id:
		State.objects.get(id=state_id).delete()
		return HttpResponseRedirect("/toast/state/")




# def index(request):
# 	list_of_meetings=Meeting.objects.all().order_by('start_datetime')
# 	return render_to_response('toast/index.html', {'list_of_meetings':list_of_meetings}, context_instance=RequestContext(request))

# @csrf_protect
# def meeting(request, meeting_id=None):
# 	if meeting_id:
# 		if request.method=='POST':
# 			form=AttendanceForm(request.POST)

# 			if form.is_valid():
# 				form.save()
# 				form=AttendanceForm
# 		else:
# 			form=AttendanceForm
# 		meeting_information=Meeting.objects.filter(id=meeting_id)[:1]
# 		attendances=Attendance.objects.filter(meeting=meeting_id)
# 		return render_to_response('toast/meeting.html', {'meeting':meeting_information[0],'attendances':attendances,'form':form,'meeting_id':meeting_id}, context_instance=RequestContext(request))
# 	else:
# 		if request.method=='POST':
# 			form=MeetingForm(request.POST)

# 			if form.is_valid():
# 				form.save()
# 				form=MeetingForm
# 		else:
# 			form=MeetingForm
# 		list_of_meetings=Meeting.objects.all().order_by('-start_datetime')
# 		return render_to_response('toast/meeting.html', {'list_of_meetings':list_of_meetings,'form':form}, context_instance=RequestContext(request))

# @csrf_protect
# def meeting_add(request):
# 	if request.method=='POST':
# 		form=MeetingForm(request.POST)

# 		if form.is_valid():
# 			new_entry=form.save()
# 			return HttpResponseRedirect('/toast/meeting/' + str(new_entry.id))
# 	else:
# 		form=MeetingForm

# 	return render_to_response('toast/meeting_add.html', {'form':form}, context_instance=RequestContext(request))






# def members(request, member_id=None):
# 	if member_id:
# 		members=Member.objects.filter(id=member_id)
# 		attendances=Attendance.objects.filter(member=member_id)
# 		return render_to_response('toast/members.html', {'members':members,'attendances':attendances}, context_instance=RequestContext(request))
# 	else:
# 		if request.method=='POST':
# 			form=MemberForm(request.POST)

# 			if form.is_valid():
# 				form.save()
# 				form=MemberForm
# 		else:
# 			form=MemberForm

# 		members=Member.objects.all().order_by('last_name')
# 		return render_to_response('toast/members.html', {'members':members,'form':form}, context_instance=RequestContext(request))

# @csrf_protect
# def member_add(request):
# 	if request.method=='POST':
# 		form=MemberForm(request.POST)

# 		if form.is_valid():
# 			form.save()
# 			return HttpResponseRedirect('/toast/members/')
# 	else:
# 		form=MemberForm
# 	return render_to_response('toast/member_add.html', {'form':form}, context_instance=RequestContext(request))






# def addresses(request, address_id=None):
# 	if address_id:
# 		addresses=Address.objects.filter(id=address_id)[:1]
# 		return render_to_response('toast/addresses.html', {'addresses':addresses}, context_instance=RequestContext(request))
# 	else:
# 		addresses=Address.objects.all().order_by('name')
# 		return render_to_response('toast/addresses.html', {'addresses':addresses}, context_instance=RequestContext(request))

# @csrf_protect
# def address_add(request):
# 	if request.method=='POST':
# 		form=AddressForm(request.POST)

# 		if form.is_valid():
# 			form.save()
# 			return HttpResponseRedirect('/toast/addresses/')
# 	else:
# 		form=AddressForm
# 	return render_to_response('toast/address_add.html', {'form':form}, context_instance=RequestContext(request))





# def sounds(request, sound_id=None):
# 	if sound_id:
# 		sounds=ParticipationType.objects.filter(id=sound_id)
# 		return render_to_response('toast/sounds.html', {'sounds':sounds}, context_instance=RequestContext(request))
# 	else:
# 		sounds=Sound.objects.all().order_by('name')
# 		return render_to_response('toast/sounds.html', {'sounds':sounds}, context_instance=RequestContext(request))

# @csrf_protect
# def sound_add(request):
# 	if request.method=='POST':
# 		form=SoundForm(request.POST)

# 		if form.is_valid():
# 			form.save()
# 			form=SoundForm
# 			#return HttpResponseRedirect('/toast/sounds/')
# 	else:
# 		form=SoundForm

# 	list_of_sounds=Sound.objects.all()
# 	return render_to_response('toast/sound_add.html', {'form':form,'list_of_sounds':list_of_sounds}, context_instance=RequestContext(request))

# @csrf_protect
# def sound_edit(request, sound_id=None):
# 	if request.method=='POST':
# 		sound=Sound.objects.get(id=sound_id)
# 		if sound:
# 			form=SoundForm(request.POST, instance=sound)

# 			if form.is_valid():
# 				form.save()
	
# 		return HttpResponseRedirect('/toast/sound_add/')
# 	else:
# 		if sound_id:
# 			sound=Sound.objects.filter(id=sound_id)[0]
# 			form=SoundForm(instance=sound)
# 			return render_to_response('toast/sound_edit.html', {'form':form}, context_instance=RequestContext(request))
# 		else:
# 			return HttpResponseRedirect('/toast/sound_add/')

# def sound_delete(request, sound_id=None):
# 	if sound_id:
# 		Sound.objects.get(id=sound_id).delete()
# 	return HttpResponseRedirect('/toast/sound_add/')





# def roles(request, role_id=None):
# 	if role_id:
# 		roles=Role.objects.filter(id=role_id)
# 		return render_to_response('toast/roles.html', {'roles':roles}, context_instance=RequestContext(request))
# 	else:
# 		roles=Role.objects.all().order_by('name')
# 		return render_to_response('toast/roles.html', {'roles':roles}, context_instance=RequestContext(request))

# @csrf_protect
# def role_add(request):
# 	if request.method=='POST':
# 		form=RoleForm(request.POST)

# 		if form.is_valid():
# 			form.save()
# 			form=RoleForm
# 	else:
# 		form=RoleForm

# 	list_of_roles=Role.objects.all()
# 	return render_to_response('toast/role_add.html', {'form':form,'list_of_roles':list_of_roles}, context_instance=RequestContext(request))

# @csrf_protect
# def role_edit(request, role_id=None):
# 	if request.method=='POST':
# 		role=Role.objects.get(id=role_id)
# 		if role:
# 			form=RoleForm(request.POST, instance=role)

# 			if form.is_valid():
# 				form.save()

# 		return HttpResponseRedirect('/toast/role_add/')
# 	else:
# 		if role_id:
# 			role=Role.objects.filter(id=role_id)[0]
# 			form=RoleForm(instance=role)
# 			return render_to_response('toast/role_edit.html', {'form':form}, context_instance=RequestContext(request))
# 		else:
# 			return HttpResponseRedirect('/toast/role_add/')

# def role_delete(request, role_id=None):
# 	if role_id:
# 		Role.objects.get(id=role_id).delete()
# 	return HttpResponseRedirect('/toast/role_add/')






# def participation_types(request, participation_type_id=None):
# 	if participation_type_id:
# 		participation_types=ParticipationType.objects.filter(id=participation_type_id)
# 		return render_to_response('toast/participation_types.html', {'participation_types':participation_types}, context_instance=RequestContext(request))
# 	else:
# 		participation_types=ParticipationType.objects.all().order_by('name')
# 		return render_to_response('toast/participation_types.html', {'participation_types':participation_types}, context_instance=RequestContext(request))

# @csrf_protect
# def participation_type_add(request):
# 	state=''
# 	if request.method=='POST':
# 		form=StateForm(request.POST)

# 		if form.is_valid():
# 			form.save()
# 			form=ParticipationTypeForm
# 	else:
# 		form=ParticipationTypeForm

# 	list_of_participation_types=ParticipationType.objects.all()
# 	return render_to_response('toast/participation_type_add.html', {'state':state,'form':form,'list_of_participation_types':list_of_participation_types}, context_instance=RequestContext(request))

# @csrf_protect
# def participation_type_edit(request, participation_type_id=None):
# 	if request.method=='POST':
# 		participation_type=ParticipationType.objects.get(id=participation_type_id)
# 		if participation_type:
# 			form=ParticipationType(request.POST, instance=participation_type)

# 			if form.is_valid():
# 				form.save()

# 		return HttpResponseRedirect('/toast/participation_type_add/')
# 	else:
# 		if participation_type_id:
# 			participation_type=ParticipationType.objects.filter(id=participation_type_id)[0]
# 			form=ParticipationTypeForm(instance=participation_type)
# 			return render_to_response('toast/particiation_type_edit.html', {'form':form}, context_instance=RequestContext(request))
# 		else:
# 			return HttpResponseRedirect('/toast/participation_type_add/')

# def participation_type_delete(request, participation_type_id=None):
# 	if participation_type_id:
# 		ParticipationType.objects.get(id=participation_type_id).delete()
# 	return HttpResponseRedirect('/toast/participation_type_add/')

# def attendance_delete(request, attendance_id=None, meeting_id=None):
# 	if attendance_id:
# 		Attendance.objects.get(id=attendance_id).delete()
# 	return HttpResponseRedirect('/toast/meeting/' + meeting_id)




# @csrf_protect
# def attendance(request, attendance_id=None):
# 	if attendance_id:
# 		if request.method=='POST':
# 			form=ParticipationForm(request.POST)

# 			if form.is_valid():
# 				process=form.save(commit=False)
# 				if process.name=='':
# 					now=datetime.now()
# 					process.name=now.strftime("%Y-%m-%d %H:%M:%S")
# 				process.save()
# 				form=ParticipationForm
# 		else:
# 			form=ParticipationForm

# 		attendance=Attendance.objects.filter(id=attendance_id)[:1]
# 		list_of_participations=Participation.objects.filter(attendance=attendance_id)
# 		return render_to_response('toast/attendance.html', {'attendance':attendance[0],'attendance_id':attendance_id,'form':form,'list_of_participations':list_of_participations}, context_instance=RequestContext(request))
# 	else:
# 		attendance=Attendance.objects.all()
# 		return render_to_response('toast/attendance.html', {'attendance':attendance}, context_instance=RequestContext(request))


# def attendance_edit(request, attendance_id=None):
# 	if request.method=='POST':
# 		referer=request.META.get('HTTP_REFERER','')
# 		attendance=Attendance.objects.get(id=attendance_id)
# 		if attendance:
# 			form=AttendanceForm(request.POST, instance=attendance)

# 			if form.is_valid():
# 				saved=form.save()
# 		return HttpResponseRedirect(referer)
# 		#return HttpResponseRedirect('/toast/meeting/'+ str(saved.meeting.id))
# 	else:
# 		if attendance_id:
# 			attendance=Attendance.objects.filter(id=attendance_id)[0]
# 			form=AttendanceForm(instance=attendance)
# 			return render_to_response('toast/attendance_edit.html', {'form':form,'attendance':attendance}, context_instance=RequestContext(request))
# 		else:
# 			return HttpResponseRedirect('/toast/attendance/' + attendance_id)



# def participations(request, participation_id=None):
# 	if participation_id:
# 		if request.method=='POST':
# 			form=TrackSoundForm(request.POST)

# 			if form.is_valid():
# 				form.save()
# 				form=TrackSoundForm
# 		else:
# 			form=TrackSoundForm

# 		participation=Participation.objects.get(id=participation_id)
# 		soundtracks=TrackSound.objects.filter(participant=participation_id)
# 		return render_to_response('toast/participations.html', {'participation':participation,'soundtracks':soundtracks,'form':form,'participation_id':participation_id}, context_instance=RequestContext(request))
# 	else:
# 		return HttpResponseRedirect('/toast/meetings/')
